package net.aaa.hotel.dao.impl;


import com.alibaba.fastjson.JSON;
import net.aaa.hotel.dao.T_sheshiDao;
import net.aaa.hotel.entity.T_TypeSheshi;
import net.aaa.hotel.entity.T_sheshi;
import net.aaa.hotel.util.BaseDB;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 凉凉
 * @time 11-11;14:49
 */

@Repository
public class T_sheshiDaoImpl implements T_sheshiDao {
    @Override
    public List<Map> findAll() {
        return BaseDB.queryMap("SELECT ssId,ssName,ssCount,name,ssPmoney,ssGys\n" +
                "FROM t_ptype tp,t_sheshi ts \n" +
                "where ts.ssTypeid= tp.id");
    }

    @Override
    public int delete(String ssId) {
        return BaseDB.update("delete from t_sheshi where ssId=?",ssId);
    }

    @Override
    public Map findByid(String ssId) {
        List<Map> list = BaseDB.queryMap("select * from t_sheshi where ssId=?", ssId);
        Map m=list.get(0);
        return m;
    }

    @Override
    public int add(T_sheshi t_sheshi) {
        if (t_sheshi.getSsId()==null){
            return BaseDB.update("insert into t_sheshi(ssName,ssCount,ssTypeid,ssPmoney,ssGys) values(?,?,?,?,?)",
                    t_sheshi.getSsName(),
                    t_sheshi.getSsCount(),
                    t_sheshi.getSsTypeid(),
                    t_sheshi.getSsPmoney(),
                    t_sheshi.getSsGys()
            );
        }else {
            return BaseDB.update("update t_sheshi set ssName=?,ssCount=?,ssTypeid=?,ssPmoney=?,ssGys=? where ssId=?",
                    t_sheshi.getSsName(),
                    t_sheshi.getSsCount(),
                    t_sheshi.getSsTypeid(),
                    t_sheshi.getSsPmoney(),
                    t_sheshi.getSsGys(),
                    t_sheshi.getSsId()
            );
        }
    }

    @Override
    public List<Map> getAllByPage(Integer curr, Integer nums,String condition) {
        String sql="SELECT ssId,ssName,ssCount,name,ssPmoney,ssGys FROM t_ptype tp,t_sheshi ts where ts.ssTypeid= tp.id ";
        if (condition!=null){
            sql+=condition;
        }
        sql+=" limit ?,? ";
        int start =(curr-1)*nums;
        return BaseDB.queryMap(sql,start,nums);
    }

    @Override
    public List<Map> getAllByPage(Integer curr, Integer nums) {
        String sql="SELECT ssId,ssName,ssCount,name,ssPmoney,ssGys FROM t_ptype tp,t_sheshi ts where ts.ssTypeid= tp.id " +
                "limit ?,?";
        int start =(curr-1)*nums;
        return BaseDB.queryMap(sql,start,nums);
    }

    

    @Override
    public int getCount(String condition) {
        String sql="select Count(*) a from t_ptype tp,t_sheshi ts where 1=1 and ts.ssTypeid= tp.id";
        if(condition!=null){
            sql+=condition;
        }
        Object a = BaseDB.queryMap(sql).get(0).get("a") + "";
        return Integer.parseInt(a+"") ;
    }

    @Override
    public int getCount() {
        String sql="select count(*) from t_sheshi";
        Object a = BaseDB.queryMap(sql).get(0).get("a");
        return Integer.parseInt(a+"");
    }
    @Override
    public List getSheshiByRoomType(Integer typeid) {
        String sql = "select s.ssName \n" +
                "from t_type t, t_sheshi s, t_roomsheshi r\n" +
                "where t.id = r.typeid and r.ssid = s.ssId and t.id = ? and ssTypeid=2";
        List<Map> type = BaseDB.queryMap(sql, typeid);
        List<String> list = new ArrayList<>();
        for (Map map : type) {
            list.add(map.get("ssName") + "");
        }
        return list;
    }





    @Override
    public List<Map> getAllGdSheShi() {
        String sql= " select * from t_sheshi where ssTypeid = 1 or  ssTypeid = 2 ";
        return BaseDB.queryMap(sql);
    }

    @Override
    public List<Map> getTypeSheshi(Integer typeid) {
        String sql="select ssid from t_roomsheshi where typeid=?";
        return BaseDB.queryMap(sql,typeid);
    }

    @Override
    public int addSheShi(T_TypeSheshi typesheshi) {
        String ssid = typesheshi.getSsid();
        int i=0;
        if(typesheshi.getSsid()!=null){
            for (String s : ssid.split(",")) {
                String sql="insert into t_roomsheshi(typeid,ssid) values(?,?)";
                i+= BaseDB.update(sql,typesheshi.getTypeid(),s);
           //     System.out.println("s"+s);
            }
        }
        return i;
    }

    @Override
    public int deleteSheshi(T_TypeSheshi typesheshi) {
        return BaseDB.update("delete from t_roomsheshi where typeid=?",typesheshi.getTypeid());
    }


}











